package com.lsht.system.service.impl.siwei;

import com.lsht.common.config.LshtConfig;
import com.lsht.common.exception.ServiceException;
import com.lsht.common.utils.FileUploadTool;
import com.lsht.common.utils.StringUtils;
import com.lsht.common.utils.uuid.IdUtils;
import com.lsht.system.domain.SysLogininfor;
import com.lsht.system.domain.siwei.SiweiDevice;
import com.lsht.system.mapper.siwei.SiweiDeviceMapper;
import com.lsht.system.service.siwei.SiweiDeviceService;
import com.lsht.system.util.NullUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 设备管理Service业务层处理
 *
 * @author zyc
 * @date 2024-08-14
 */
@Service
public class SiweiDeviceServiceImpl implements SiweiDeviceService {
    @Autowired
    private SiweiDeviceMapper siweiDeviceMapper;

    /**
     * 新增设备管理
     *
     * @param siweiDevice 设备管理
     * @param file
     * @return 结果
     */
    @Override
    public int genAdd(SiweiDevice siweiDevice, MultipartFile file) {

        siweiDevice.setCreateTime(new Date());
//        siweiPointDetails.setCreateTime(new Date());
        final String logoUrl = LshtConfig.getProfile() + File.separator + "device" + File.separator;
        if (NullUtil.isNotEmpty(file)) {
            //设置文件路径
            String originalFilename = file.getOriginalFilename();
            // 上传并返回新文件名称
            try {
                String fileName = FileUploadTool.upload(logoUrl, file, originalFilename);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String replace = logoUrl.replace(LshtConfig.getProfile(), "/profile") + originalFilename;
            siweiDevice.setDeviceUrl(replace.replaceAll("\\\\", "/"));
        }else {
            siweiDevice.setDeviceUrl(null);
        }
        //id 不为空走修改
        if (NullUtil.isNotEmpty(siweiDevice.getId())) {
            //若用户是admin 则设置userId为null(因为用户id也跟着修改了,变成1的话  用户就无法与地图匹配了)
            return siweiDeviceMapper.genUpdateById(siweiDevice);
        }
        siweiDevice.setId(IdUtils.simpleUUID());
//        String s = InspectionRecordGenerator.generateInspectionCode();
//        siweiDevice.setDeviceNum(s);
        return siweiDeviceMapper.genAdd(siweiDevice);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int genDeleteById(List<String> idList) {
        return siweiDeviceMapper.genDeleteById(idList);
    }

    /**
     * 修改设备管理
     *
     * @param siweiDevice 设备管理
     * @return 结果
     */
    @Override
    public int genUpdateById(SiweiDevice siweiDevice) {
        siweiDevice.setUpdateTime(new Date());
        return siweiDeviceMapper.genUpdateById(siweiDevice);
    }

    /**
     * 查询设备管理列表
     *
     * @param siweiDevice 设备管理
     * @return 设备管理
     */
    @Override
    public List<SiweiDevice> genQueryList(SiweiDevice siweiDevice) {
        List<String> date = siweiDevice.getDate();
        if (NullUtil.isNotNull(date) && date.size() > 0) {
            siweiDevice.setBeginTime(date.get(0));
            siweiDevice.setEndTime(date.get(1));
        }
        return siweiDeviceMapper.genQueryList(siweiDevice);
    }

    /**
     * 根据id查询设备详情
     *
     * @param siweiDevice
     * @return
     */
    @Override
    public SiweiDevice genQueryById(SiweiDevice siweiDevice) {
        return siweiDeviceMapper.genQueryById(siweiDevice);
    }

    /**
     * 大屏查询设备数量及昨日新增数
     *
     * @param siweiDevice
     * @return
     */
    @Override
    public HashMap deviceStatistics(SiweiDevice siweiDevice) {
        HashMap<Object, Object> map = new HashMap<>();
        //查询设备数量
        Integer deviceNum = siweiDeviceMapper.getCountDeviceNum();
        map.put("deviceNum", deviceNum);
        //获取昨日开始和结束时间
        String[] yesterdayStartAndEndTime = SiweiDateUtils.getYesterdayStartAndEndTime();
        //查询设备数量昨日日新增数
        Integer deviceAddNum = siweiDeviceMapper.getNumByYesterday(yesterdayStartAndEndTime[0], yesterdayStartAndEndTime[1]);
        map.put("deviceAddNum", deviceAddNum);
        return map;
    }

    @Override
    public HashMap systemStatistics(SysLogininfor sysLogininfor) {
        HashMap<Object, Object> map = new HashMap<>();
        //查询设备数量
        Integer systemNum = siweiDeviceMapper.getCountSystemNum();
        map.put("systemNum", systemNum);
        //获取昨日开始和结束时间
        String[] yesterdayStartAndEndTime = SiweiDateUtils.getYesterdayStartAndEndTime();
        //查询设备数量昨日日新增数
        Integer systemAddNum = siweiDeviceMapper.getNumBySystemYesterday(yesterdayStartAndEndTime[0], yesterdayStartAndEndTime[1]);
        map.put("systemAddNum", systemAddNum);
        return map;
    }


    @Override
    public String importDevice(List<SiweiDevice> siweiDeviceList, String operName) {
        if (StringUtils.isNull(siweiDeviceList) || siweiDeviceList.size() == 0) {
            throw new ServiceException("导入设备数据不能为空！");
        }
        StringBuilder successMsg = new StringBuilder();
        int successNum = 0;
        for (SiweiDevice siweiDevice : siweiDeviceList) {
            siweiDevice.setId(IdUtils.simpleUUID());
            siweiDevice.setCreateTime(new Date());
            siweiDevice.setUseStatus(0);
            siweiDeviceMapper.genAdd(siweiDevice);
            successNum++;
            successMsg.append("<br/>" + successNum + "、设备 " + siweiDevice.getDeviceNumber() + " 导入成功");
        }
        return successMsg.toString();
    }

}
